package com.panda.cn.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panda.cn.config.exception.RestfulException;
import com.panda.cn.entity.SysJobEntity;
import com.panda.cn.entity.SysRoleEntity;
import com.panda.cn.mapper.SysJobMapper;
import com.panda.cn.service.SysJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统岗位服务impl
 *
 * @author Yanmh
 * @date 2022/09/20
 */
@Slf4j
@Service
@DS("sys")
public class SysJobServiceImpl implements SysJobService {
    
    @Resource
    private SysJobMapper sysJobMapper;
    
    /**
     * 页面列表
     *
     * @return {@link List}<{@link SysRoleEntity}>
     */
    @Override
    public Page<SysJobEntity> pageList(JSONObject params) {
        Long current = params.getLong("current");
        Long size = params.getLong("size");
        log.info("当前页数>>>{}", current);
        log.info("当前页显示数量>>>{}", size);
        Page<SysJobEntity> page = new Page<>(current, size);
        LambdaQueryWrapper<SysJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysJobEntity::getIsDelete, "N")
                .like(SysJobEntity::getName, params.getStr("name"))
                .like(SysJobEntity::getCode, params.getStr("code"))
                .orderByDesc(SysJobEntity::getUpdateTime);
        return  sysJobMapper.selectPage(page, queryWrapper);
    }
    
    /**
     * 插入
     *
     * @param params 参数个数
     * @return {@link Long}
     */
    @Override
    public Long insert(SysJobEntity params) {
        // 判断是否为空
        if(StrUtil.isEmpty(params.getName()) || StrUtil.isEmpty(params.getCode()))
            throw new RestfulException("名称或编码不能为空");
        // 查看是不是存在相同的编码和名称
        LambdaQueryWrapper<SysJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysJobEntity::getName, params.getName())
                .or()
                .eq(SysJobEntity::getCode, params.getCode());
        Integer count = sysJobMapper.selectCount(queryWrapper);
        if(count>0)
            throw new RestfulException("名称或编码存在相同");
        
        // 当前时间
        DateTime nowDateTime = DateUtil.parse(DateUtil.now());
        params.setCreateTime(nowDateTime);
        params.setUpdateTime(nowDateTime);
        // 添加
        sysJobMapper.insert(params);
        return params.getId();
    }
    
    @Override
    public Long update(SysJobEntity params) {
        // 判断是否为空
        if (ObjUtil.isEmpty(params.getId()))
            throw new RestfulException("唯一编码缺失");
        if(StrUtil.isEmpty(params.getName()) || StrUtil.isEmpty(params.getCode()))
            throw new RestfulException("名称或编码不能为空");
        // 查看是不是存在相同的编码和名称
        LambdaQueryWrapper<SysJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(SysJobEntity::getId, params.getId())
                .and(i -> i.eq(SysJobEntity::getName, params.getName())
                        .or()
                        .eq(SysJobEntity::getCode, params.getCode()));
                
        Integer count = sysJobMapper.selectCount(queryWrapper);
        if(count>0)
            throw new RestfulException("名称或编码存在相同");
        
        // 当前时间
        DateTime nowDateTime = DateUtil.parse(DateUtil.now());
        params.setUpdateTime(nowDateTime);
        // 添加
        sysJobMapper.updateById(params);
        
        return params.getId();
    }
    
    /**
     * 删除
     *
     * @param params 参数个数
     * @return {@link Long}
     */
    @Override
    public Long remove(List<Long> params) {
        // 验证
        if (ObjUtil.isNull(params) || params.size() == 0)
            throw new RestfulException("唯一编码缺失");
        //  更新的编码
        LambdaQueryWrapper<SysJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysJobEntity::getId, params);
        
        // 更新的数据
        SysJobEntity entity = new SysJobEntity();
        entity.setIsDelete("Y");
    
        int update = sysJobMapper.update(entity, queryWrapper);
        return Long.valueOf(update);
    }
    
    /**
     * 所有
     *
     * @return {@link List}
     */
    @Override
    public List all() {
        LambdaQueryWrapper<SysJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysJobEntity::getIsDelete, "N");
        return sysJobMapper.selectList(queryWrapper);
    }
    
    
}
